﻿using Avalonia.Controls;
using LightCAD.Core;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Threading.Tasks;

namespace LightCAD.Runtime
{
    public class DocumentManager
    {
        public static string DefaultDrawingName = "图纸";
        public static int DrawingIndex = 1;
        public static List<LcDocument> Documents = new List<LcDocument>();
        public static Dictionary<LcDocument, DocumentRecorder> Recorders = new Dictionary<LcDocument, DocumentRecorder>();
        public static LcDocument Current { get; private set; }
        public static DocumentRecorder CurrentRecorder { get; private set; }

        public static string GetAutoName()
        {
            return $"{DefaultDrawingName}{DrawingIndex}.ldwg";
        }
        public static LcDocument Create()
        {
            var doc=  new LcDocument();
            doc.FilePath = GetAutoName();
            doc.Initialize();
            return doc;
        }

        public static void SetCurrent(LcDocument document)
        {
            if (!Documents.Contains(document))
            {
                Documents.Add(document);
            }
            Current=document;
            LcDocument.SetCurrent(document);

            if (Recorders.ContainsKey(document))
            {
                CurrentRecorder = Recorders[document];
            }
            else
            {
                CurrentRecorder = new DocumentRecorder(document);
                Recorders.Add(document, CurrentRecorder);
            }
        }

        public static LcDocument LoadDocument( string filePath)
        {
            var zipFile = ZipFile.OpenRead(filePath);
            var zipDict = zipFile.Entries.ToDictionary((e) => e.FullName);
            var drawingEntry = zipDict[Constants.DefaultDrawingName];
            LcDocument doc = null;
            using (StreamReader reader = new StreamReader(drawingEntry.Open()))
            {
                var json = reader.ReadToEnd();
                doc = LcDocument.FromJson(json);
                doc.FilePath = filePath;
                return doc;
            }
        }
        public static void SaveDocument(LcDocument document,string filePath)
        {
            var options = new JsonSerializerOptions
            {
                ReferenceHandler = ReferenceHandler.Preserve,
                DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
                WriteIndented = true
            };

            //图形JSON写入到ZIP文件中
            var start = DateTime.Now;
            using (FileStream zipFile = new FileStream(document.FilePath, FileMode.OpenOrCreate))
            using (ZipArchive archive = new ZipArchive(zipFile, ZipArchiveMode.Create))
            {
                ZipArchiveEntry readMeEntry = archive.CreateEntry(Constants.DefaultDrawingName);
                using (StreamWriter writer = new StreamWriter(readMeEntry.Open()))
                {
                    var json = LcDocument.ToJson(document, false);
                    writer.Write(json);
                }
            }
        }
    }
}
